<?php
namespace app\api\controller;

use app\common\controller\Api;
use  think\db;

/**
 * 示例接口
 */
class Merchlist extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    /**
     * @return Auth  date,cateid
     */
    public function getmymerch(){

        $uid=$this->auth->id;

        $show=$this->request->param('show');

        $where['a.user_id']=['eq',$uid];
        $where['a.status']=['eq',$show];



        $merch = Db::name('merch_lists')->alias('a')
            ->join('fa_merch_cate b', 'a.cate_id = b.id', 'LEFT')
            ->where($where)
            ->order('a.gettime desc')
            ->field('a.*, b.name as cate_name')
            ->select();

//        echo Db::getLastSql();


        foreach ($merch as $key => $value){
            $merch[$key]['gettime']=date('Y-m-d H:i',$value['gettime']);

        }

        $this->success('',$merch);

    }


    /**
     * 跟进记录
     */
    public function getmymerchlist(){

        $uid=$this->auth->id;

        $id=$this->request->param('id');

        $cate=Db::name('merch_lists')->where(['id'=>$id])->find();

        $this->success('',$cate);

    }


    /**
     * 跟进记录
     */

    public function merchlistslog(){
        $uid=$this->auth->id;

        $id=$this->request->param('id');

        $follow=Db::name('merch_lists_log')->where(['lists_id'=>$id])->select();


        foreach ($follow as $key => $value){
            $follow[$key]['createtime']=date('Y-m-d H:i:s',$value['createtime']);
        }

        $this->success('',$follow);

    }

    /**
     * 添加跟进
     */

    public function addmerchlistslog(){


        $data['desc']=$this->request->param('str');
        $data['createtime']=time();
        $data['lists_id']=$this->request->param('id');
        $data['user_id']=$this->auth->id;

        Db::name('merch_lists_log')->data($data)->insert();

        $this->success('添加跟进');

    }

    /**
     * 修改备注
     */

    public function setmerchlisttypes(){

        $id=$this->request->param('merch_id');

        $types=$this->request->param('desc');
        $ret=Db::name('merch_lists')->where(['id'=>$id])->update(['desc'=>$types]);
        if ($ret){
            $this->success('success');
        }else{
            $this->error('error');
        }

    }


    /**
        修改类型
     */
    public function setmerchlistcate(){

        $id=$this->request->param('merch_id');
        $cate_id=$this->request->param('cate_id');

        $cate=Db::name('merch_cate')->where(['id'=>$cate_id])->find();



        $ret=Db::name('merch_lists')
            ->where(['id'=>$id])
            ->update(['cate_id'=>$cate_id,'status'=>$cate['status']]);



        if ($ret){
            $this->success('success');
        }else{
            $this->error('error');
        }

    }


    /**
     *  数据信息
     */
    public  function getmerchlistinfo(){

        $count1=Db::name('merch_lists')->where(['user_id'=>$this->auth->id,'status'=>0])->count();
        $count2=Db::name('merch_lists')->where(['user_id'=>$this->auth->id,'status'=>1])->count();
        $count3=Db::name('merch_lists')->where(['user_id'=>$this->auth->id,'status'=>2])->count();
        $count4=Db::name('merch_lists')->where(['user_id'=>$this->auth->id,'status'=>3])->count();


        $this->success('success',['count1'=>$count1,'count2'=>$count2,'count3'=>$count3,'count4'=>$count4]);



    }

}